package com.chapter13.homework;

/**
 * @author 宋鑫宇
 * @version 1.0
 * 实现字符串翻转
 */
public class HomeWork01 {
    public static void main(String[] args) {
        String str = "abcdef";
        try {
           str =  reverse(str,1,5);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        System.out.println("转换后===============");
        System.out.println(str);
    }

    public static String reverse(String str, int start, int end) {
        //做验证防止抛出异常
        //编程思想：先想正确的情况然后取反
        if (!(str != null && start > 0 && end > start&&end<str.length())) {
            throw new RuntimeException("输入参数有误");
        }
        char[] a = str.toCharArray();
        char temp = ' ';
        for (int i = start, j = end; i < j; i++, j--) {
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
        return new String(a);
    }
}
